import 'package:flutter/material.dart';

class TextFieldPage extends StatefulWidget {
  TextFieldPage({Key key}) : super(key: key);

  // @override
  _TextFieldPageState createState() => _TextFieldPageState();
}

class _TextFieldPageState extends State<TextFieldPage> {
  var _username = new TextEditingController(); // 初始化时给表单赋值
  var _password;

  @override
  void initState() {
    super.initState();
    _username.text = '初始值';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("textField"),
        actions: <Widget>[
          IconButton(icon: Icon(Icons.settings), onPressed: () {})
        ],
      ),
      body: Padding(
          padding: EdgeInsets.all(20),
          // child: TextDemo(), demo
          child: Column(children: <Widget>[
            TextField(
              decoration: InputDecoration(
                hintText: '请输入用户名',
              ),
              controller: _username,
              onChanged: (value) {
                setState(() {
                  _username.text = value;
                });
              },
            ),
            SizedBox(height: 20),
            TextField(
              obscureText: true, // 密码黑点
              decoration: InputDecoration(
                hintText: '请输入密码',
              ),
              controller: _username,
              onChanged: (value) {
                setState(() {
                  this._password = value;
                });
              },
            ),
            SizedBox(height: 20),
            Container(
              width: double.infinity, // 宽度自适应
              child: RaisedButton(
                child: Text('登录'),
                onPressed: () {
                  print(this._username.text);
                  print(this._password);
                },
                color: Colors.blue,
                textColor: Colors.white,
              ),
            )
          ])),
    );
  }
}

class TextDemo extends StatelessWidget {
  const TextDemo({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          TextField(),
          SizedBox(height: 20),
          TextField(
            decoration: InputDecoration(
                hintText: '请输入搜索的内容', border: OutlineInputBorder()),
          ),
          SizedBox(height: 20),
          TextField(
            obscureText: true,
            decoration:
                InputDecoration(hintText: '密码框', border: OutlineInputBorder()),
          ),
          SizedBox(height: 20),
          TextField(
            decoration:
                InputDecoration(labelText: '用户名', border: OutlineInputBorder()),
          ),
          SizedBox(height: 20),
          TextField(
            obscureText: true,
            decoration:
                InputDecoration(labelText: '密码', border: OutlineInputBorder()),
          ),
          SizedBox(height: 20),
          TextField(
            obscureText: true,
            decoration: InputDecoration(
              icon: Icon(Icons.people),
              labelText: '请输入用户名',
            ),
          ),
        ],
      ),
    );
  }
}
